package com.behome.netty.netty_B.bean;


import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.common.CapCmdManager;
import lombok.Data;

import java.nio.ByteBuffer;

@Data
public class ReBackPacketBody {
    static final int RESOURCE_CODE_LENGTH = 12;
    private String sourceCode;
    private String[] targetCodes;
    private BusinessReBackBase businessReBack;

    public ReBackPacketBody() {
    }

    public ReBackPacketBody(String sourceCode, String[] targetCodes, BusinessReBackBase businessReBack) {
        this.sourceCode = sourceCode;
        this.targetCodes = targetCodes;
        this.businessReBack = businessReBack;
    }

    public byte[] serialize() {
        byte[] businessCmdData = this.businessReBack.serialize();
        int length = 14 + 12 * (this.targetCodes == null ? 0 : this.targetCodes.length) + 1 + 2 + businessCmdData.length;
        ByteBuffer byteBuffer = ByteBuffer.allocate(length);
        byteBuffer.put(ByteConventerKit.strToBcd(this.sourceCode)).put(ByteConventerKit.shortToBytes((new Integer((this.targetCodes == null ? 0 : this.targetCodes.length))).shortValue()));
        if (this.targetCodes != null) {
            String[] var4 = this.targetCodes;
            for (String code : var4) {
                byteBuffer.put(ByteConventerKit.strToBcd(code));
            }
        }
        byteBuffer.put(this.businessReBack.getReBackTypeValue())
                .put(ByteConventerKit.shortToBytes((new Integer(businessCmdData.length)).shortValue()))
                .put(businessCmdData);
        return byteBuffer.array();
    }

    public static ReBackPacketBody deserialize(byte[] bodyData) {
        ReBackPacketBody ret = new ReBackPacketBody();
        if (bodyData != null || bodyData.length > 0) {
            ByteBuffer byteBuffer = ByteBuffer.wrap(bodyData);
            byte[] sourceCodeData = new byte[12];
            byteBuffer.get(sourceCodeData);
            String sourceCode = ByteConventerKit.deserializeResourceCode(sourceCodeData);
            ret.setSourceCode(sourceCode);
            short targetCount = byteBuffer.getShort();
            byte[] targetCodes = new byte[targetCount * 12];
            if (targetCodes.length > 0) {
                byteBuffer.get(targetCodes);
                ByteBuffer tmpBuffer = ByteBuffer.wrap(targetCodes);
                String[] targetCodeStrs = new String[targetCount];

                for (int i = 0; i < targetCount; ++i) {
                    byte[] tmpB = new byte[12];
                    tmpBuffer.get(tmpB);
                    targetCodeStrs[i] = ByteConventerKit.deserializeResourceCode(tmpB);
                }

                ret.setTargetCodes(targetCodeStrs);
            }

            byte businessCmdType = byteBuffer.get();
            short businessCmdLength = byteBuffer.getShort();
            byte[] businessCmdData = new byte[businessCmdLength];
            byteBuffer.get(businessCmdData);
            Class<BusinessReBackBase> cmdClass = CapCmdManager.getProtocolBClass(businessCmdType);
            if (cmdClass != null) {
                BusinessReBackBase busiCmdBase = null;
                try {
                    busiCmdBase = cmdClass.newInstance();
                } catch (InstantiationException | IllegalAccessException var13) {
                    var13.printStackTrace();
                }
                ret.setBusinessReBack(busiCmdBase.deserialize(businessCmdData));
            }
        }

        return ret;
    }
}
